package sanshui.system.trade.factory.notify;

import lombok.extern.slf4j.Slf4j;
import sanshui.system.trade.common.GlobalContain;
import sanshui.system.trade.factory.trade.strategy.StrategyCallType;
import sanshui.system.trade.factory.trade.strategy.StrategyResult;
import sanshui.system.trade.model.LogNotifyMsgModel;
import sanshui.system.trade.util.CommonTimeUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 通过不同的通知类型，选择模板制作通知内容
 * */
@Slf4j
public class LogNotifyFactory {
    private final static List<LogNotifyMsgModel> logNotifyList = new ArrayList<>();
    private final static int MAX_LOG_NOTIFY_NUM = 100;

    /**
     * 制作内容
     * 格式：TXT
     * */
    public static String getNotifyContentInfoByBsWithTxt(List<StrategyResult> strategyResults){
        StringBuilder contentBuy = new StringBuilder();
        StringBuilder contentSell = new StringBuilder();
        for (StrategyResult strategyResult : strategyResults) {
            if (StrategyCallType.BUY.compareTo(strategyResult.getStrategyCallType()) == 0){
                log.debug("买入, 策略:{}, code:{}, price:{}, num:{}", strategyResult.getStrategyCode().getMessage(), strategyResult.getCode(), strategyResult.getPrice(), strategyResult.getNum());
                contentBuy.append(String.format("买入, 策略:%s, code:%s, price:%s, num:%s \n", strategyResult.getStrategyCode().getMessage(), strategyResult.getCode(), strategyResult.getPrice(), strategyResult.getNum()));
            }
            if (StrategyCallType.SELL.compareTo(strategyResult.getStrategyCallType()) == 0){
                log.debug("卖出, 策略:{}, code:{}, price:{}, num:{}", strategyResult.getStrategyCode().getMessage(), strategyResult.getCode(), strategyResult.getPrice(), strategyResult.getNum());
                contentSell.append(String.format("卖出, 策略:%s, code:%s, price:%s, num:%s \n", strategyResult.getStrategyCode().getMessage(), strategyResult.getCode(), strategyResult.getPrice(), strategyResult.getNum()));
            }
        }
        return contentBuy.append(contentSell).toString();
    }

    public static void addLogNotify(Integer userId, Integer accId, Integer workerId, String msg){
        LogNotifyMsgModel logNotifyMsgModel = new LogNotifyMsgModel();
        logNotifyMsgModel.setMsg(msg);
        logNotifyMsgModel.setAccId(accId);
        logNotifyMsgModel.setUserId(userId);
        logNotifyMsgModel.setWorkerId(workerId);
        logNotifyMsgModel.setDatetime16(CommonTimeUtil.getNowTime16());
        logNotifyList.add(logNotifyMsgModel);
        if (logNotifyList.size() > MAX_LOG_NOTIFY_NUM){
            logNotifyList.remove(0);
        }
    }
    public static void addLogNotify(LogNotifyMsgModel msgModel ){
        logNotifyList.add(msgModel);
        if (logNotifyList.size() > MAX_LOG_NOTIFY_NUM){
            logNotifyList.remove(0);
        }
    }

    public static List<LogNotifyMsgModel> findLogNotifyMsg(int num){
        if (num >= logNotifyList.size()){
            return logNotifyList;
        }
        return logNotifyList.subList(0, num);
    }
}
